<template>
  <div class="header">
    <div class="header-container">
      <ul class="header-link">
        <template v-if="username">
          <li>
            <div class="header-user">
              <div class="header-username" @mouseover="showDropDown('isUserDropDown')" @mouseout="hideDropDown('isUserDropDown')">
                <div class="header-username-top" :class="{ 'header-dropdown-top-hover': isUserDropDown }">
                	<div class="header-username-content">
                		{{ username }}
                  	<i class="icon-arrow-down"></i>
                	</div>
                </div>
                <ul class="header-username-dropdown" v-if="isUserDropDown">
                  <router-link to="/personal/account/security" tag="li">
                      {{$t('message.accountSet ')}}
                  </router-link>
                  <li @click="logout">
                      {{$t('message.signOut')}}
                  </li>
                </ul>
              </div>
              <div class="header-message">
                <div class="header-message-text">
                  <router-link class="header-hover" to="/personal/message/notice">{{ $t('message.message') }}</router-link>
                </div>
                <div class="header-message-icon" v-if="unreadMsg > 0">
                  {{ unreadMsg }}
                </div>
              </div>
            </div>
          </li>
        </template>
        <template v-else>
          <li>
            <router-link to="/login" class="header-login">{{$tc('message.login', 1)}}</router-link>
            <router-link to="/register" class="header-register">{{$t('message.register')}}</router-link>
          </li>
        </template>
        <li>
          <router-link class="header-hover" to="/help">{{$t('message.help')}}</router-link>
        </li>
        <li class="header-no-right">
          <div class="header-dropdown" @mouseover="showDropDown('isDropDownShow')" @mouseout="hideDropDown('isDropDownShow')">
            <div class="header-dropdown-top" :class="{ 'header-dropdown-top-hover': isDropDownShow }">
              <div class="header-content" :class="{'china' : language === '中文', 'english' : language === 'English', 'thai' : language === 'ภาษาไทย'}">
              	{{ language }}
              	<i class="icon-arrow-down"></i>
              </div>
            </div>
            <ul class="header-dropdown-menu" v-if="isDropDownShow">
              <li @click="chooseChina" v-if="language !== '中文'">
                 <div class="header-dropdown-item china">中文</div>
              </li>
              <li @click="chooseEnglish" v-if="language !== 'English'">
                <div class="header-dropdown-item english">English</div>
              </li>
              <li @click="chooseThai" v-if="language !== 'ภาษาไทย'">
                <div class="header-dropdown-item thai">ภาษาไทย</div>
              </li>
            </ul>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getCookie, delCookie } from '@/util/util'

export default {
  name: 'TheHeaderTop',
  data () {
    return {
      isUserDropDown: false,
      isDropDownShow: false,
      language: 'ภาษาไทย'
    }
  },
  watch: {
    $router: 'checkLogin'
  },
  computed: {
    username () {
      return this.$store.state.loginUser.username
    },
    unreadMsg () {
      return this.$store.state.loginUser.unreadMsg
    }
  },
  methods: {
    chooseThai () {
      this.language = 'ภาษาไทย'
      this.isDropDownShow = false
      this.$i18n.locale = 'thai'
      window.localStorage.setItem('thaiPayMallLanguage', 'thai')
      this.$router.go(0)
    },
    chooseEnglish () {
      this.language = 'English'
      this.isDropDownShow = false
      this.$i18n.locale = 'en'
      window.localStorage.setItem('thaiPayMallLanguage', 'en')
      this.$router.go(0)
    },
    chooseChina () {
      this.language = '中文'
      this.isDropDownShow = false
      this.$i18n.locale = 'cn'
      window.localStorage.setItem('thaiPayMallLanguage', 'cn')
      this.$router.go(0)
    },
    // 显示个人Hover框
    showDropDown (val) {
      clearTimeout(this['timer' + val])
      this[val] = true
    },
    // mouseout后500ms隐藏hover框
    hideDropDown (val) {
      this['timer' + val] = setTimeout(() => {
        this[val] = false
      }, 500)
    },
    logout () {
      this.$ajax.loginOut().then((res) => {
        delCookie('username')
        delCookie('profile')
        delCookie('unreadMsg')
        this.$store.commit('clearLoginUser')
        let name = this.$route.name
        if (name !== 'DetailProduct' || name !== 'DetailParam' || name !== 'Index') {
          this.$router.push({ path: '/' })
        }
      })
    },
    checkLogin () {
      let username = getCookie('username')
      let profile = getCookie('profile')
      let unreadMsg = getCookie('unreadMsg')
      if (username) {
        this.$store.commit('updateLoginUser', {
          username,
          profile,
          unreadMsg
        })
      } else {
        console.log('用户没有登录')
      }
    },
    init () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      if (t) {
        if (t === 'cn') {
          this.language = '中文'
        } else if (t === 'en') {
          this.language = 'English'
        } else {
          this.language = 'ภาษาไทย'
        }
      } else {
        this.language = 'ภาษาไทย'
      }
    }
  },
  created () {
    this.checkLogin()
    this.init()
  }
}
</script>

<style scoped>
.header {
  height: 40px;
  min-width: 1200px;
  background-color: #f5f5f5;
  z-index: 3;
}

.header-container {
  width: 1200px;
  height: 40px;
  margin: 0 auto;
}

.header-link {
  float: right;
}

.header-link > li {
	height: 40px;
	line-height: 40px;
	float: left;
	border-right: 1px solid transparent;
	padding: 0 18px;
	font-size: 12px;
	position: relative;
}

.header-link > li:after {
	content: '';
	display: block;
	width: 0;
	height: 13px;
	border-right: 1px solid #c1c1c1;
	position: absolute;
	top: 14px;
	right: -1px;
}

.header-link .header-no-right {
  border-right: none;
  padding: 0;
}

.header-link .header-no-right:after {
	display: none;
}

.header-link a {
  color: #333;
}

.header-login {
  margin-right: 24px;
}

.header-link .header-register {
  color: #ff3535;
}

.header-user:after {
  content: '';
  display: block;
  width: 0;
  height: 0;
  visibility: hidden;
  clear: both;
}

.header-username {
  float: left;
  margin-right: 6px;
  position: relative;
}

.header-username:hover .icon-arrow-down {
	transform: rotate(180deg);
}

.header-username-top {
  padding: 0 17px;
  border-left: 1px solid transparent;
  border-right: 1px solid transparent;
  cursor: pointer;
}

.header-username-content {
	min-width: 40px;
	padding-right: 16px;
	text-align: center;
	position: relative;
}

.icon-arrow-down {
  display: block;
  width: 0;
  height: 0;
  border-top: 5px solid #333;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  position: absolute;
  top: 18px;
  right: 0;
  transition: all .3s;
}

.header-username-dropdown {
  width: 100%;
  position: absolute;
  top: 100%;
  left: 0;
  border: 1px solid #e6e6e6;
  border-top: 0;
  box-sizing: border-box;
  background-color: #fff;
}

.header-username-dropdown li {
  text-align: center;
  cursor: pointer;
}

.header-username-dropdown li:hover {
	color: #ff600a;
}

.header-message {
  float: left;
}

.header-message:after {
  content: '';
  display: block;
  width: 0;
  height: 0;
  visibility: hidden;
  clear: both;
}

.header-message-text {
  float: left;
  margin-right: 4px;
}

.header-message-icon {
  float: left;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: #ff600a;
  color: #fff;
  text-align: center;
  line-height: 15px;
  margin-top: 13px;
}

.header-dropdown {
  height: 40px;
}

.header-dropdown:hover .icon-arrow-down {
	transform: rotate(180deg);
}

.header-dropdown-top {
  height: 40px;
  padding: 0 17px;
  border-left: 1px solid transparent;
  border-right: 1px solid transparent;
  cursor: pointer;
}

.header-dropdown-top-hover {
	color: #ff600a;
	background-color: #fff;
	border-left: 1px solid #e6e6e6;
  border-right: 1px solid #e6e6e6;
}

.header-content {
	padding: 0 16px 0 40px;
	position: relative;
}

.china{
  background: url(../../assets/images/icon_flag_cn.png) no-repeat left center;
}
.english{
  background: url(../../assets/images/icon_flag_en.png) no-repeat left center;
}
.thai{
  background: url(../../assets/images/icon_flag_th.png) no-repeat left center;
}

.header-dropdown-menu {
  width: 100%;
  position: absolute;
  top: 100%;
  left: 0;
  border: 1px solid #e6e6e6;
  border-top: 0;
  box-sizing: border-box;
  padding: 0 17px;
  background-color: #fff;
}

.header-dropdown-menu > li {
	cursor: pointer;
}

.header-dropdown-menu > li:hover {
	color: #ff600a;
}

.header-dropdown-item {
  padding-left: 30px;
  text-align: center;
}

.header-link .header-hover:hover {
	color: #ff600a;
}
</style>
